<template>
  <div >
    <transition name="fade">
      <div class="v-modal table-mask" v-if="showEdit"  @click="hiden"></div>
    </transition>
    <transition name="bf-horizontal" >
      <div class="bf-table-right-module"  v-if="showEdit" v-bind:style="{ width: width + 'px', left: left + 'px', right: right + 'px'   }">
        <div class="clearfix bf-table-right-module-title shadow-b">
          <div class="float-l"><span class="bar-l"></span>{{title?title:'新增'}}</div>
          <div class="float-r" @click="hiden"><img src="../../../assets/beneform4j/home/icon_close.png" alt=""></div>
        </div>

        <div class="bf-table-right-form">
          <div>
            <el-form ref="form" :model="form" :rules="editRules"  label-position="right" label-width="110px" class="clearfix">
              <div  v-for="item in editFormFilter" :key="item.field" v-if="!item.hideEdit" v-bind:style="{ width: item.onlyRow? 100 : 100/rowNum + '%' }" class="bf-table-form-item padding-l padding-r">
                <component v-if="!item.slot" v-bind:is="'bf-form-'+item.type" :item="item" :disabled="item.disableEdit" :form="form" :rules="editRules"></component>
                <slot v-if="item.slot" :name="item.slot"></slot>
              </div>
            </el-form>
          </div>
        </div>
        <div class="bf-table-right-module-bottom shadow-t">
          <div v-if="!submitButtons">
            <el-button type="primary" @click="sure">{{actionTitle}}</el-button>
            <el-button @click="hiden">取消</el-button>
          </div>
          <div v-if="submitButtons">
            <el-button @click="doCustomEvent(item)" v-for="item in submitButtons" :key="item.title">{{item.title}}</el-button>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'bfTableForm',
  props: ['editForm', 'editRules', 'submitButtons', 'width', 'rowNum', 'title', 'left', 'right'],
  data() {
    return {
      showEdit: false,
      form: {},
      actionTitle: '确定',
      userId: ''
    }
  },
  methods: {
    hiden() {
      this.showEdit = false
      document.body.style.overflow = 'initial'
    },
    hidden() {
      this.hiden()
    },
    add() {
      this.clearValue()
      this.showDom()
      this.actionTitle = '新增'
      this.userId = false
    },
    show(scope) {
      this.clearValue()
      if (scope && scope.row) {
        this.form = { ...scope.row }
      } else {
        this.form = { ...scope }
      }
      this.showDom()
    },
    showDom() {
      this.showEdit = true
      document.body.style.overflow = 'hidden'
    },
    clearValue() {
      this.form = {}
    },
    sure() {
      delete this.form['_orgid']
      this.validate()
    },
    validate() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('formSubmit', this.form, this.editForm)
        } else {
          this.$message({
            message: '请按照正确的格式填写表单',
            type: 'error'
          })
          return false
        }
      })
    },
    doCustomEvent(item) {
      if (item.validate === false) {
        item.click(this.form)
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          item.click(this.form)
        } else {
          this.$message({
            message: '请按照正确的格式填写表单',
            type: 'error'
          })
          return false
        }
      })
    },
  },
  watch: {
    form: {
      handler: function(v, oldV){
        if (this.editForm) {
          this.editForm.forEach(v => {
            if (v.formChange) {
              v.formChange(this.form, v, this.editForm)
            }
            if (v.showEditSituation) {
              const showItem = v.showEditSituation(this.form, oldV)
              v.hideEdit = !showItem
            }
          })
        }
      },
      deep: true
    }
  },
  mounted: function() {
  },
  computed: {
    editFormFilter: function() {
      return this.editForm.filter((v) => {
        if (v.showEditSituation) {
          const showItem = v.showEditSituation(this.form)
          v.hideEdit = !showItem
        }
        if (v.type) {
          return true
        }
      })
    }
  }

}
</script>

<style scoped>
  .table-mask{
    z-index: 2003;
  }
</style>
